<template>
  <div class="app-container">
    <el-form ref="ruleForm" :rules="rules" :model="form" label-width="120px">
      <el-form-item label="部门编号" prop="deptCode">
        <el-input v-model="form.deptCode" />
      </el-form-item>

      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="form.deptName" />
      </el-form-item>

      <el-form-item label="上级部门ID" prop="superiorDeptId">
        <el-input v-model="form.superiorDeptId" />
      </el-form-item>

      <el-form-item label="备注" prop="reMark">
        <el-input v-model="form.reMark" />
      </el-form-item>

      <!--  <el-form-item label="地区名称" prop="areaName">
        <el-input v-model="form.areaName" />
      </el-form-item>
 -->
      <el-form-item label="地区名称" prop="areaName">
        <span>
          {{ form.areaName }}
          <el-button size="mini" style="padding:5px 7px;margin-left: 10px;" @click="areaShow = true;">修改</el-button>
        </span>
        <v-distpicker v-if="areaShow" @province="onChangeProvince" @city="onChangeCity" @area="onChangeArea" />
      </el-form-item>

      <!--  <el-form-item label="角色" prop="RoleName">
        <el-select v-model="form.RoleName" filterable placeholder="请选择角色" style="width: 100%;">
          <el-option v-for="(item, index) in roleList" :key="index" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item> -->

    </el-form>

    <div style="text-align: center;">
      <el-button type="primary" @click="onSubmit('ruleForm')">确认</el-button>
      <el-button @click="onCancel">关闭</el-button>
    </div>

  </div>
</template>

<script>
import request from '@/utils/request'
import qs from 'qs';
import axios from 'axios'
import VDistpicker from 'v-distpicker'

export default {
  components: { VDistpicker },
  props: ['id'],
  data() {
    return {
      institutionList: [],
      roleList: [],
      form: {},
      areaShow: false,
      buttonLoading: false,
      rules: {
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        roleId: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ],
        institutionId: [
          { required: true, message: '请选择单位', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getDeptById();
    // this.getRoleList();
    // this.getInstitutionList();
  },
  mounted() {

  },
  methods: {
    getDeptById() {
      var userId = sessionStorage.getItem('userId');
      this.$axios({
        url: '/sysDept/selectDeptById',
        method: 'post',
        data: qs.stringify({ token: 'abc123', userId: userId, deptId: this.id })
      }).then((res) => {
        this.form = res.data.data
      }).catch(() => {
        // console.log(err)
      })
    },
    getRoleList() {
      request({
        url: '/role/getRoleList',
        method: 'get',
        params: {}
      }).then((res) => {
        this.roleList = res.result;
      }).catch((err) => {
        this.$message(err)
      })
    },
    getInstitutionList() {
      request({
        url: '/institution/getInstitutionList',
        method: 'get',
        params: {}
      }).then((res) => {
        this.institutionList = res.result;
      }).catch((err) => {
        this.$message(err)
      })
    },
    onSubmit(ruleForm) {
      var userId = sessionStorage.getItem('userId');
      var userName = sessionStorage.getItem('userName');
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          this.form.token = 'abc123';
          this.form.userId = userId,
          this.form.uName = userName,
          this.$axios({
            url: '/sysDept/updateDept',
            method: 'post',
            data: qs.stringify(this.form)
          }).then((res) => {
            if (res.data.status == 200) {
              this.$emit('closeDialog', false);
              this.$message(res.data.data)
              this.$emit('refresh')
            } else {
            	this.$message(res.data.data)
            }
          }).catch(() => {
            this.buttonLoading = false;
          })
        } else {
          this.$message({
            message: '表单验证失败',
            type: 'warning'
          })

          return false;
        }
      });
    },
    // 获取省市区信息赋值给form
    onChangeProvince(a) {
      this.form.areaName = a.value;
      this.form.areaId = a.code;
    },
    onChangeCity(a) {
      this.form.areaName = this.form.areaName + a.value;
      this.form.areaId = a.code;
    },
    onChangeArea(a) {
      this.form.areaName = this.form.areaName + a.value;
      this.form.areaId = a.code;
    },
    // //////////////
    onCancel() {
      this.$emit('closeDialog', false);
    }
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>
